import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:ls_work/message/message_config.dart';
import 'package:ls_work/net/api/http_api.dart';
import 'package:ls_work/net/post_request.dart';
import 'package:ls_work/net/response/response_data.dart';
import 'package:ls_work/net_configs.dart';

import '../../net/response/patrol/patrol_detail_data.dart';
import '../../net/response/patrol/patrol_site_list.dart';

class PatrolNfcWriteViewModel extends BaseListViewModel<PatrolSiteList> {
  final String isWrite;

  PatrolNfcWriteViewModel(BuildContext context, this.isWrite) : super(context);

  var keyword = "";
  var model = PatrolModel();

  @override
  Future<List<PatrolSiteList>> requestListData() async {
    return await model.getPatrolList(page, pageLength,
        nameLike: keyword, isWrite: isWrite);
  }

  @override
  void onCreated() {
    super.onCreated();
    requestRefresh();
  }

  @override
  void receiveMessage(SendMessageEvent event) {
    super.receiveMessage(event);
    if (event.msgCode == MessageConfig.REFRESH_PATROL) {
      requestRefresh();
    }
  }
}

class PatrolModel extends BaseMvvmModel {
  @override
  void onCleared() {}

  Future<List<PatrolSiteList>> getPatrolList(int page, int pageLength,
      {String? nameLike, String? isWrite}) async {
    var map = {
      "page": page,
      "rows": pageLength,
      "nameLike": nameLike,
      "isWrite": isWrite
    };
    var data = await HttpGo.instance.postData(PatrolApi.patrolSiteList,
        data: map, options: urlencodedHeader);
    var bean = ResponseBean.fromJson(data);
    if ("0" != bean.code) {
      throw bean.msg??"";
    }
    var list =
        ((bean.data as List?)??[]).map((e) => PatrolSiteList.fromJson(e)).toList();
    return list;
  }

  ///更新写入状态
  Future<Map> updatePatrol(
      {required String id, required String isWrite}) async {
    var map = {"id": id, "isWrite": isWrite};
    var data = await HttpGo.instance.postData(PatrolApi.updatePatrol,
        data: map.toJsonMapStr(), options: urlencodedHeader);
    if ("0" == data['code']) {
      return data;
    } else {
      throw data['msg'];
    }
  }

  Future<Map> getPatrolNum() async {
    var data =
        await HttpGo.instance.postData(PatrolApi.getPatrolNumber, data: {});
    if ("0" == data['code']) {
      return data;
    } else {
      throw data['msg'];
    }
  }


}
